
package com.huaweicloud.haydn.delivercore.agent.factory;

import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @since 2022-12-07
 */
public enum RejectExecHandlerEnum {
    CALLER_RUNS("CallerRuns"),
    ABORT("Abort"),
    DISCARD("Discard"),
    DISCARD_OLDEST("DiscardOldest");

    private final String name;

    RejectExecHandlerEnum(String name) {
        this.name = name;
    }

    public RejectedExecutionHandler handler() {
        if (name.equals(CALLER_RUNS.name)) {
            return new ThreadPoolExecutor.CallerRunsPolicy();
        }
        if (name.equals(ABORT.name)) {
            return new ThreadPoolExecutor.AbortPolicy();
        }
        if (name.equals(DISCARD.name)) {
            return new ThreadPoolExecutor.DiscardPolicy();
        }
        if (name.equals(DISCARD_OLDEST.name)) {
            return new ThreadPoolExecutor.DiscardOldestPolicy();
        }
        return new ThreadPoolExecutor.DiscardPolicy();
    }
}
